package myproject0608;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JProgressBar;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.border.BevelBorder;

public class FileCopy extends JFrame implements ActionListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JButton[] bt = new JButton[2];
	private JTextField[] tf = new JTextField[2];
	private File srcFile;   
	private JButton cpbt;
	private JProgressBar pb;
	Font fon = new Font("微软雅黑", Font.BOLD, 18);

	public FileCopy() { // 创建空布局主界面
		setVisible(true);
		setSize(800, 800);
		setLocation(600, 100);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setTitle("选择文件 ");
		setLayout(null);

		String[] str = { "选择文件", "存放位置" };

		for (int i = 0; i < str.length; i++) {
			JLabel label = new JLabel(str[i]);
			label.setBounds(50, 50 + i * 40, 70, 30);
			add(label);
			tf[i] = new JTextField();
			tf[i].setFont(fon);
			tf[i].setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
			tf[i].setBounds(120, 50 + i * 40, 300, 30);
			add(tf[i]);
			bt[i] = new JButton("...");
			bt[i].setBackground(new Color(0x7FFF00));
			bt[i].setFont(fon);
			bt[i].setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
			bt[i].setBounds(430, 50 + i * 40, 60, 30);
			bt[i].addActionListener(this);
			add(bt[i]);
		}

		cpbt = new JButton("复制");           //设置复制按钮
		cpbt.setFont(fon);
		cpbt.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
		cpbt.setBackground(new Color(0x00E5EE));
		cpbt.setBounds(300, 600, 100, 30);
		cpbt.addActionListener(this);        //监听复制按钮
		add(cpbt);

		pb = new JProgressBar(0, 100);      //设置进度条(0-100)
		pb.setStringPainted(true);      //显示进度条的进度可见
		// pb.setValue(60);
		pb.setSize(600, 25);
		pb.setLocation(100, 320);
		add(pb);

	}

	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {

			public void run() {
				new FileCopy();

			}
		});
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		Object o = e.getSource();     //通过事件e创建 点击对象o
		if (o == bt[0]) {            
			JFileChooser fc = new JFileChooser();   //设置文件选择对象fc
			int f = fc.showOpenDialog(this);       //变量f等于fc当前的选择
			if (f == JFileChooser.APPROVE_OPTION) {  //如果f等于文件选择打开
				srcFile = fc.getSelectedFile();     //srcFile得到选择的文件
				tf[0].setText(srcFile.getPath());   //选择文件框输出选择文件的路线

			}

		} else if (o == bt[1]) {
			JFileChooser fc = new JFileChooser();
			fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);  //设置文件选择模式只能是文件夹
			int f = fc.showOpenDialog(this);
			if (f == JFileChooser.APPROVE_OPTION) {
				tf[1].setText(fc.getSelectedFile().getPath());
			}
		}

		 else if(o==cpbt) {
			    
			 String filename=srcFile.getName();
			 File desFile =new File(tf[1].getText()+"\\"+filename);  //新建目标文件 
			 new Thread(new Runnable() {         // 创建一个线程
				public void run() {
				try {
					BufferedInputStream bis =new BufferedInputStream(new FileInputStream(srcFile));    //创建一个读取对象,从源文件scrFile中读取
					BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(desFile));   //创建一个写入对象,写入到目标文件desFile
					int a = bis.read();          //变量a保存读取到内容 
					long filelen=srcFile.length();    //进度条的长度等于源文件的长度
					long count=0;                     //设置进度条的进度默认为0
					int percent;                     //进度条
					while(a!=-1){                  //当读取的内容不等于-1时循环
						count++;                   
						percent=(int)(1.0*count/filelen*100);//进度条=进度/长度（*1.0是为了自动转换为double类型，前面加int强制转换为int类型）
						pb.setValue(percent);      //进度条的值的等于percent
						bos.write(a);               //写入的的内容是a中保的内容
						a=bis.read();               //a循环读取
					}
					bis.close();bos.flush();bos.close();//循环结束把bit关闭，bos刷新然后关闭
					
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				
					
					
					
				}
			}).start();  //启动线程
			
			 
			 
			 
			 
			 
			 
			 
			 
		 }
	}
}
